/*
 * @Author: CPS
 * @Date: 2020-07-25 18:01:11
 * @Last Modified by: CPS
 * @Last Modified time: 2020-08-24 17:44:37
 * @Projectname Electron-fileWatcher
 * @Filename notificationToWindow.js
 */
'use strict'
import template from './notificationToWindow.pug'
import './notificationToWindow.stylus'

import iconClose from '!!svg-inline-loader!./icon/u90.svg'
import iconFolder from '!!svg-inline-loader!./icon/u168.svg'

/**
 * 定义本插件的前缀名字
 *
 * @param      {string}  name    The name
 * @return     {string}  { description_of_the_return_value }
 */
function prefix(name) {
  return 'noificationToWindow__' + name
}

/**
 * 创建弹窗对象
 */
class notifiObject {
  constructor(target) {
    this.render = template
    this.settings = { // 默认配置
      icon: {
        svg: {
          '关闭按钮': iconClose,
          '文件夹': iconFolder
        }
      },

      msg: {
        title: '(161332) 翁瑞珠 打印3本',
        content: '文件夹被创建了!!!'
      }, //消息内容

      max: 3, //最大同时存在的通知数量
      limit: 1, //保留多少条不自动清除
      dir: 0, //通知生成的方向
      delay: 4000, //定时清除间隔
    }
    this.el = null
    this.id = 0
    this.history = []

    // 传入的tar可以时一个html对象{作为父元素},也可以时一个字符串{id}
    if (target) {
      switch (target.toString()) {
        case '[object HTMLElement]':
          //传入的是一个element实例,当作父元素挂载

          //target 当作 本体
          if (target.id == prefix('component')) {
            this.el = target
            break;
          }

          //target 当作 父元素
          this.el = document.createElement('section')
          this.el.id = this.el.className = prefix('component')
          target.appendChild(this.el)
          break;

        case '':
          //传入了空值，尝试查找文档下是否存在 component 元素
          console.log('传入了空值')
          this.el = document.getElementById(prefix('component'))
          if (!this.el) {
            console.log('发现目标元素')
            this.el = isElement
          } else {
            this.el = document.createElement('section')
            this.el.id = this.el.className = prefix('component')
            document.body.appendChild(this.el)
          }
          break;

        default:
          //target 为字符串时，当作id名字处理
          this.el = document.getElementById(target.toString());
          if (!this.el) return
          break;
      }
    }

    if (!this.el) return //如果创建el失败，则返回false

    // 创建事件
    // 关闭当前通知
    this.el.addEventListener('click', (e) => {
      switch (e.target.className) {
        case prefix('btn-close'):
          console.log('点击了关闭按钮')
          this.el.removeChild(e.target.parentNode)
          break;
        default:
          console.log('我被点击了:' + e.target.className)
          break;
      }
    })

    this.pushMsg('test')
    return this
  }

  // 发送信息函数
  pushMsg(msg, newSettings) {
    if (msg) Object.assign(this.settings.msg.title = msg)
    if (newSettings) Object.assign(this.settings, newSettings)

    console.log('开始发送消息')
    // 超过指定数量,进行删减
    if (this.el.childElementCount >= this.settings.max) {
      this.el.firstElementChild.style.opacity = 0
      this.el.removeChild(this.el.firstElementChild)
    }

    // 将消息添加到历史记录
    if (this.settings.msg) {
      this.history.push(this.settings.msg.toString())
    }

    let tar
    // 根据不同方向,生成标签
    this.el.innerHTML = this.el.innerHTML + this.render(this.settings)
    tar = this.el.lastElementChild
    setTimeout((tar) => {
      tar.style.opacity = 1
    }, 50, tar)


    this.delMsgInterval(this.el)
  }

  // 定时清除不需要的通知
  delMsgInterval(tar) {
    if (this.id) return //不重复添加监听器

    //设置每隔一段事件删除旧的通知
    this.id = setInterval(() => {
      if (tar.childElementCount < this.settings.limit + 1) {
        clearInterval(this.id)
        this.id = 0
      } else {
        tar.firstElementChild.style.opacity = 0
        setTimeout(() => {
          tar.removeChild(tar.firstElementChild)
        }, 500, tar)
      }
    }, this.settings.delay, tar)
  }
}

export default (target) => {
  return new notifiObject(target)
}